package loveqq.niceexcel.converter.internal.biginteger;

import cn.hutool.core.util.StrUtil;
import loveqq.niceexcel.annotation.internal.Internal;
import loveqq.niceexcel.converter.CellData;
import loveqq.niceexcel.converter.ConvertContext;
import loveqq.niceexcel.converter.Converter;
import loveqq.niceexcel.util.NumberUtils;
import org.apache.poi.ss.usermodel.CellType;

import java.math.BigInteger;
import java.text.ParseException;

/**
 * @author zhongjh@tsintergy.com
 * @date 2022-08-18
 */
@Internal
public class BigIntegerStringConverter implements Converter<BigInteger> {
    @Override
    public Class<BigInteger> supportJavaType() {
        return BigInteger.class;
    }

    @Override
    public CellType supportExcelType() {
        return CellType.STRING;
    }

    @Override
    public BigInteger convertToJavaData(CellData<?> cellData, ConvertContext context) throws ParseException {
        String value = cellData.getStringValue();
        return StrUtil.isEmpty(value) ? null : NumberUtils.parseBigDecimal(value, context.getNumberFormat()).toBigInteger();
    }

    @Override
    public CellData<?> convertToExcelData(BigInteger data, ConvertContext context) {
        String value = (data == null) ? null : NumberUtils.format(data, context.getNumberFormat());
        return new CellData<>(value);
    }
}
